package com.sdp.right.db.sys_emp.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdp.core.bizc.redis.RedisUtil;
import com.sdp.core.bizc.utils.CoreConstantsEnum;
import com.sdp.core.bizc.utils.TokenUtils;
import com.sdp.core.bizc.vo.BusinessContext;
import com.sdp.right.db.sys_emp.mapper.SysEmpMapper;
import com.sdp.right.db.sys_emp.service.ISysEmpService;
import com.sdp.right.db.sys_menu.service.ISysMenuService;
import com.sdp.right.db.sys_menu_button.service.ISysMenuButtonService;
import com.sdp.right.db.sys_role.service.ISysRoleService;
import com.sdp.right.db.sys_role_person.service.ISysRolePersonService;
import com.sdp.right.entity.SysEmp;
import com.sdp.right.utils.RightConstants;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zsp
 * @since 2023-10-12
 */
@Service
@DS(value="right")
public class SysEmpServiceImpl extends ServiceImpl<SysEmpMapper, SysEmp> implements ISysEmpService {
	
	@Autowired
	private RedisUtil redisUtil;
	
	@Autowired
	private ISysMenuService iSysMenuService;
	
	@Autowired
	private ISysRolePersonService iSysRolePersonService;
	
	@Autowired
	private ISysRoleService iSysRoleService;
	
	@Autowired
	private ISysMenuButtonService iSysMenuButtonService;

	@Override
	public BusinessContext login(SysEmp sysEmp) throws Exception {
		BusinessContext businessContext = new BusinessContext();
		if(sysEmp==null || StringUtils.isEmpty(sysEmp.getAccountNo()) || StringUtils.isEmpty(sysEmp.getEmpPwd())) {
			businessContext.setSuccess(false);
			businessContext.setMessage("账号和密码不能为空");
			return businessContext;
		}
		sysEmp = querySysEmp(sysEmp.getAccountNo(), sysEmp.getEmpPwd());
		if(sysEmp==null) {
			businessContext.setSuccess(false);
			businessContext.setMessage("账号密码输入错误");
			return businessContext;
		}else if(sysEmp.getIsUse().equals(RightConstants.否.getCode())){
			businessContext.setSuccess(false);
			businessContext.setMessage("此账号已锁定，不能登录");
			return businessContext;
		}
		
		boolean isSet = redisUtil.setMap(SysEmp.class, sysEmp.getAccountNo(), sysEmp);
		if(!isSet) {
			businessContext.setSuccess(false);
			businessContext.setMessage("请确认缓存服务已经开启");
			return businessContext;
		}
		//生成token
//		JSONObject token = new JSONObject();
//		token.put("token", TokenUtils.token(sysEmp.getAccountNo(),sysEmp.getEmpPwd(),sysEmp.getEmpNo(),sysEmp.getEmpName()));
		businessContext.setSuccess(true);
		businessContext.put("token",TokenUtils.token(sysEmp.getAccountNo(),sysEmp.getEmpPwd(),sysEmp.getEmpNo(),sysEmp.getEmpName()));
		businessContext.put(sysEmp.getClass(), sysEmp);
		return businessContext;
	}

	@Override
	public SysEmp querySysEmp(String accountNo, String pwd) {
		LambdaQueryWrapper<SysEmp> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysEmp::getAccountNo, accountNo);
		queryWrapper.eq(SysEmp::getEmpPwd, pwd);
		return getOne(queryWrapper,false);
	}

	@Override
	public BusinessContext permissionInfo(boolean rightMode,String token,String systemNo) {
		BusinessContext businessContext = new BusinessContext();
		if(StringUtils.isEmpty(token)) {
			businessContext.setSuccess(false);
			businessContext.setMessage("授权已过期，请重新登陆");
			return businessContext;
		}
		JSONObject obj = TokenUtils.verify(token);
		
		if(obj==null || StringUtils.isEmpty(obj.get(CoreConstantsEnum.token中账号的属性名.getCode())) || StringUtils.isEmpty(obj.get(CoreConstantsEnum.token中密码的属性名.getCode()))) {
			businessContext.setSuccess(false);
			businessContext.setMessage("登陆已过期，请重新登录");
			return businessContext;
		}
		SysEmp sysEmp = querySysEmp(obj.getString(CoreConstantsEnum.token中账号的属性名.getCode()), obj.getString(CoreConstantsEnum.token中密码的属性名.getCode()));
		if(sysEmp==null) {
			businessContext.setSuccess(false);
			businessContext.setMessage("账号密码输入错误");
			return businessContext;
		}else if(sysEmp.getIsUse().equals(RightConstants.否.getCode())){
			businessContext.setSuccess(false);
			businessContext.setMessage("此账号已锁定，不能登录");
			return businessContext;
		}
		
		boolean isSet = redisUtil.setMap(SysEmp.class, sysEmp.getAccountNo(), sysEmp);
		if(!isSet) {
			businessContext.setSuccess(false);
			businessContext.setMessage("请确认缓存服务已经开启");
			return businessContext;
		}
		
		List<String> roleNos = iSysRolePersonService.queryRoleNos(sysEmp.getAccountNo());
		if(roleNos==null || roleNos.size()==0) {
			businessContext.setSuccess(false);
			businessContext.setMessage("此账号没有登录权限，请先分配角色");
			return businessContext;
		}
		
			
		JSONObject permissionInfo = new JSONObject();
		permissionInfo.put("token", TokenUtils.token(sysEmp.getAccountNo(),sysEmp.getEmpPwd(),sysEmp.getEmpNo(),sysEmp.getEmpName()));
		permissionInfo.put("sysEmp", sysEmp);
		permissionInfo.put("empName", sysEmp.getEmpName());
		permissionInfo.put("empNo", sysEmp.getEmpNo());
		permissionInfo.put("accountNo", sysEmp.getAccountNo());
		permissionInfo.put("menus",iSysMenuService.createRouterTree(rightMode,sysEmp.getAccountNo(), systemNo));
		permissionInfo.put("roles", iSysRoleService.querySysRole(roleNos));
		permissionInfo.put("buttons", iSysMenuButtonService.querySysMenuButtonNos(roleNos, systemNo));
		permissionInfo.put("profilePicture",sysEmp.getProfilePicture());
		businessContext.setSuccess(true);
		businessContext.put("permissionInfo", permissionInfo);
		return businessContext;
	}

	@Override
	public boolean changePwd(SysEmp sysEmp) {
		LambdaUpdateWrapper<SysEmp> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(SysEmp::getAccountNo, sysEmp.getAccountNo()).set(SysEmp::getEmpPwd,sysEmp.getEmpPwd());
        return update(new SysEmp(), lambdaUpdateWrapper);
	}

	@Override
	public SysEmp querySysEmpByEmpNo(String empNo) {
		LambdaQueryWrapper<SysEmp> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysEmp::getEmpNo, empNo);
		return getOne(queryWrapper,false);
	}

	@Override
	public SysEmp querySysEmpByAccountNo(String accountNo) {
		LambdaQueryWrapper<SysEmp> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysEmp::getAccountNo, accountNo);
		return getOne(queryWrapper,false);
	}

	@Override
	public void removeSysEmpByAccountNo(String accountNo) {
		LambdaQueryWrapper<SysEmp> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysEmp::getAccountNo, accountNo);
		remove(queryWrapper);
		
	}

	@Override
	public List<SysEmp> querySysEmpByAccountNos(List<String> accountNos) {
		LambdaQueryWrapper<SysEmp> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.in(SysEmp::getAccountNo, accountNos);
		return list(queryWrapper);
	}

}
